package com.bkgy.modularbx.dictionary.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bkgy.commom.model.QueryDTO;
import com.bkgy.config.BusinessException;
import com.bkgy.modularbx.commonbx.BxCommonService;
import com.bkgy.modularbx.dictionary.domain.DataDictionary;
import com.bkgy.modularbx.dictionary.dto.DataDictionaryDto;
import com.bkgy.modularbx.dictionary.mapper.DataDictionaryMapper;
import com.bkgy.modularbx.dictionary.service.DataDictionaryService;
import com.bkgy.sys.common.entity.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.Optional;

/**
 * 数据字典服务实现类
 */
@Service
@Transactional
public class DataDictionaryServiceImpl extends ServiceImpl<DataDictionaryMapper, DataDictionary>
        implements DataDictionaryService {

    @Autowired
    private BxCommonService commonService;

    private static final String NAME = "DATA_DICTIONARY";

    @Override
    public ResultData<Void> addDictionary(DataDictionary dataDictionary) {
        try {
            dataDictionary.setToc(new Date());
            dataDictionary.setTom(new Date());
            Optional<Integer> maxIndocno = Optional.ofNullable(commonService.getMaxIndocno(NAME));
            dataDictionary.setIndocno(maxIndocno.orElse(null));
            save(dataDictionary);
            return ResultData.ResultDataSuccessSelf("添加成功", null);
        } catch (Exception e) {
            throw new BusinessException("添加失败");
        }
    }

    @Override
    public ResultData<Page<DataDictionary>> pageDictionary(QueryDTO<DataDictionaryDto> queryDTO) {
        DataDictionaryDto dto = queryDTO.getDto();
        LambdaQueryWrapper<DataDictionary> wrapper = new LambdaQueryWrapper<>();
        // 构建查询条件
        if (dto != null) {
            wrapper.like(StringUtils.hasText(dto.getProductionLine()), DataDictionary::getProductionLine, dto.getProductionLine());
//            wrapper.like(StringUtils.hasText(dto.getAreaCode()),DataDictionary::getAreaCode, dto.getAreaCode());
            wrapper.like(StringUtils.hasText(dto.getDicVariableName()), DataDictionary::getDicVariableName, dto.getDicVariableName());
//            wrapper.like(StringUtils.hasText(dto.getDeviceCode()),DataDictionary::getDeviceCode, dto.getDeviceCode());
            wrapper.like(StringUtils.hasText(dto.getDicVariableDescription()), DataDictionary::getDicVariableDescription, dto.getDicVariableDescription());
        }

        Page<DataDictionary> page = new Page<>(queryDTO.getPage().getPageIndex(), queryDTO.getPage().getPageSize());
        Page<DataDictionary> result = page(page, wrapper);
        return ResultData.ResultDataSuccess(result, (int) result.getTotal());
    }

    @Override
    public ResultData<Void> deleteDictionary(Integer indocno) {
        DataDictionary dictionary = getById(indocno);
        if (dictionary == null) {
            throw new BusinessException("要删除的数据字典不存在");
        }
        removeById(indocno);
        return ResultData.ResultDataSuccessSelf("删除成功", null);
    }


    @Override
    public ResultData<Void> updateDictionary(DataDictionary dataDictionary) {
        DataDictionary existDictionary = getById(dataDictionary.getIndocno());
        if (existDictionary == null) {
            throw new BusinessException("要修改的数据字典不存在");
        }
        dataDictionary.setTom(new Date());
        updateById(dataDictionary);
        return ResultData.ResultDataSuccessSelf("修改成功", null);
    }

    @Override
    public ResultData<DataDictionary> findById(Integer indocno) {
        DataDictionary dataDictionary = getById(indocno);
        if (dataDictionary == null) {
            throw new BusinessException("数据字典不存在");
        }
        return ResultData.ResultDataSuccess(dataDictionary, 1);
    }
}




